package com.ruoyi.complex.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.complex.domain.ComplexDirectory;
import com.ruoyi.complex.domain.ComplexItem;
import com.ruoyi.complex.service.IComplexDirectoryService;
import com.ruoyi.complex.service.impl.ComplexDirectoryServiceImpl;
import com.ruoyi.complex.service.impl.ComplexItemServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 综测目录Controller
 * 
 * @author wky
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/complex/directory")
@Tag(name = "【综测目录】管理")
public class ComplexDirectoryController extends BaseController {
    @Autowired
    private ComplexDirectoryServiceImpl complexDirectoryService;

    @Autowired
    private ComplexItemServiceImpl complexItemService;

    /**
     * 查询综测目录列表
     */
    @Operation(summary = "查询综测目录列表")
    @PreAuthorize("@ss.hasPermi('complex:directory:list')")
    @GetMapping("/list")
    public AjaxResult list(ComplexDirectory complexDirectory) {
        List<ComplexDirectory> list = complexDirectoryService.selectComplexDirectoryList(complexDirectory);
        return success(list);
    }

    /**
     * 导出综测目录列表
     */
    @Operation(summary = "导出综测目录列表")
    @PreAuthorize("@ss.hasPermi('complex:directory:export')")
    @Log(title = "综测目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComplexDirectory complexDirectory) {
        List<ComplexDirectory> list = complexDirectoryService.selectComplexDirectoryList(complexDirectory);
        ExcelUtil<ComplexDirectory> util = new ExcelUtil<ComplexDirectory>(ComplexDirectory.class);
        util.exportExcel(response, list, "综测目录数据");
    }

    /**
     * 获取综测目录详细信息
     */
    @Operation(summary = "获取综测目录详细信息")
    @PreAuthorize("@ss.hasPermi('complex:directory:query')")
    @GetMapping(value = "/{directoryId}")
    public AjaxResult getInfo(@PathVariable("directoryId") Long directoryId) {
        return success(complexDirectoryService.selectComplexDirectoryByDirectoryId(directoryId));
    }

    /**
     * 新增综测目录
     */
    @Operation(summary = "新增综测目录")
    @PreAuthorize("@ss.hasPermi('complex:directory:add')")
    @Log(title = "综测目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComplexDirectory complexDirectory) {
        if (complexDirectory.getParentId().equals(Long.valueOf(0L))) {
            complexDirectory.setAncestorName(complexDirectory.getDirectoryName());
        }
        return toAjax(complexDirectoryService.insertComplexDirectory(complexDirectory));
    }

    /**
     * 修改综测目录
     */
    @Operation(summary = "修改综测目录")
    @PreAuthorize("@ss.hasPermi('complex:directory:edit')")
    @Log(title = "综测目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComplexDirectory complexDirectory) {
        if (complexDirectory.getParentId().equals(Long.valueOf(0L))
                && StringUtils.isNotEmpty(complexDirectory.getAncestorName())) {
            complexDirectoryService.updateByAncestorName(complexDirectory);
            ComplexItem complexItem = new ComplexItem();
            complexItem.setAncestorName(complexDirectory.getAncestorName());
            complexItemService.updateByAncestorName(complexItem);
        }
        return toAjax(complexDirectoryService.updateComplexDirectory(complexDirectory));
    }

    /**
     * 删除综测目录
     */
    @Operation(summary = "删除综测目录")
    @PreAuthorize("@ss.hasPermi('complex:directory:remove')")
    @Log(title = "综测目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{directoryIds}")
    public AjaxResult remove(@PathVariable(name = "directoryIds") Long[] directoryIds) {
        return toAjax(complexDirectoryService.deleteComplexDirectoryByDirectoryIds(directoryIds));
    }

}
